package com.lmolong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmolong.authbean.AuthUserDetail;
import com.lmolong.mapper.TUserMapper;
import com.lmolong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.lmolong.pojo.TUser;

@Primary
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TUserMapper tUserMapper;

    @Override
    public AuthUserDetail loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser param =new TUser();
        param.setUsername(username);
        TUser tUser = tUserMapper.selectOne(new QueryWrapper<>(param));
        if(tUser==null){
            throw new UsernameNotFoundException("用户不存在");
        }else{
            return UserDetailConverter.convert(tUser);
        }
    }

    private static class UserDetailConverter {
        static AuthUserDetail convert(TUser user) {
            return new AuthUserDetail(user);
        }
    }
}
